package com.yogee.topseller.activity.login.view.impl;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.AppManager;
import com.yogee.core.utils.LogUtils;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.base.HttpActivity;
import com.yogee.topseller.activity.login.model.bean.CheckBean;
import com.yogee.topseller.activity.login.model.bean.SMSbean;
import com.yogee.topseller.activity.login.model.bean.UserBean;
import com.yogee.topseller.activity.login.presenter.RegisterPresenter;
import com.yogee.topseller.activity.login.view.IRegisterView;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.fragment.FreeFragment;
import com.yogee.topseller.fragment.TiyanFragment;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.Constants;
import com.yogee.topseller.view.ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;



public class ResetPsActivity extends HttpActivity implements IRegisterView {
    @BindView(R.id.re_back)
    ImageView back;
    @BindView(R.id.sure)
    TextView sure;
    @BindView(R.id.new_password)
    ClearEditText newPassword;
    @BindView(R.id.new_password_sure)
    ClearEditText newPasswordSure;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.ll_fwxy)
    LinearLayout ll_fwxy;
    @BindView(R.id.login_title)
    TextView login_title;
    private String phoneNum;
    private String flag = "";
    private RegisterPresenter registerPresenter;

    private String openid="",name="", headerUrl="",deviceId="",unionid="";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reset_ps;
    }

    @Override
    protected void initView() {
        flag = getIntent().getStringExtra("flag");
        flag = getIntent().getStringExtra("flag");
        if(flag.equals(Constants.FLAG_REGIST)){
            tv_title.setText("注册");
        }else if(flag.equals(Constants.FLAG_FORGETPASSWORD)){
            tv_title.setText("忘记密码");
            ll_fwxy.setVisibility(View.GONE);
        }else{
            tv_title.setText("关联手机号码");
            login_title.setVisibility(View.GONE);
            openid = getIntent().getStringExtra("openid");
            name = getIntent().getStringExtra("name");
            headerUrl = getIntent().getStringExtra("headerUrl");
            deviceId = getIntent().getStringExtra("deviceId");
            unionid = getIntent().getStringExtra("unionid");
        }

        phoneNum = getIntent().getStringExtra("phoneNum");
        registerPresenter = new RegisterPresenter(this);

    }

    @OnClick({R.id.re_back, R.id.sure,R.id.login_title,R.id.img_wx,R.id.ll_fwxy})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.re_back:
                finish();
                break;
            case R.id.sure:

                if(flag.equals(Constants.FLAG_FORGETPASSWORD)){
                    MobclickAgent.onEvent(ResetPsActivity.this, "forget_inputPwd_sureBTn");
                }else if(flag.equals(Constants.FLAG_REGIST)){
                    MobclickAgent.onEvent(ResetPsActivity.this, "reg_inputPwd_suerBtn");
                }else{
                    MobclickAgent.onEvent(ResetPsActivity.this, "wx_reg_inputpwd_sure");
                }
                String newPsd = newPassword.getText().toString();
                String newPsdSure = newPasswordSure.getText().toString();

                if(!"".equals(newPsd)&&newPsd!=null){
                    if(newPsd.length()<6){
                        ToastUtils.showToast(this, "至少输入6位密码");
                    }else if(newPsd.length()>20){
                        ToastUtils.showToast(this, "密码长度不能超过20位");
                    }else if(newPsd.equals(newPsdSure)){
                        if(pd!=null)
                            pd.show();
                        if(flag.equals(Constants.FLAG_FORGETPASSWORD))
                            updatePassword(phoneNum, newPassword.getText().toString());
                        // 注册
                        else if(flag.equals(Constants.FLAG_REGIST)){
                            registerPresenter.newUserRegistration(phoneNum
                                    , newPsd
                                    // 第三个参数为 管家编号
                                    , "");
                            // 第三方授权
                        }else{
                            registerWX(newPsd);
                        }

                    }else{
                        if(TextUtils.isEmpty(newPsdSure)){
                            ToastUtils.showToast(this, "再次输入密码不能为空");
                        }else
                            ToastUtils.showToast(this, "两次输入的密码不一致");
                    }
                }else{
                    ToastUtils.showToast(this, "请输入密码");
                }
//                if (newPsd.length() >= 4 && newPsd.length() <= 20) {
//                    updatePassword(phoneNum, newPassword.getText().toString());
//                } else {
//                    ToastUtils.showToast(this, "您输入的密码长度不正确");
//                }
                break;
            case R.id.login_title:
                MobclickAgent.onEvent(ResetPsActivity.this, "loginBtn","登录");
                startActivity(new Intent(ResetPsActivity.this, LoginActivity.class));
                break;
            case R.id.img_wx:
                ToastUtils.showToast(this, "微信操作");
                break;
            case R.id.ll_fwxy:
                startActivity(new Intent(ResetPsActivity.this, TermOfService.class));
                break;
            default:
                break;
        }
    }

    private void registerWX(String pwd){
        HttpManager.getInstance().otherLogin1(openid,unionid,name,headerUrl,deviceId,phoneNum,pwd).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<UserBean>() {
            @Override
            public void onNext(UserBean o) {
//                ToastUtils.showToast(ResetPsActivity.this,"");
                if (pd != null)
                    pd.dismiss();
                MobclickAgent.onEvent(ResetPsActivity.this, "wx_reg_inputpwd_sure_success");
                AppUtil.saveUserInfo(ResetPsActivity.this, o);
                if(o.getFlag().equals("0")){
                    TiyanFragment freeFragment = new TiyanFragment();
                    Bundle args = new Bundle();
                    args.putString("day", o.getDay());
                    freeFragment.setArguments(args);
                    freeFragment.setCancelable(false);
                    freeFragment.show(getFragmentManager(),"");
                }else{
                    AppManager.finishAllActivity();
                    Intent intent = new Intent(ResetPsActivity.this, RegisterLabelActivity.class);
                    intent.putExtra("userId", o.getUserId());
                    startActivity(intent);
                    finish();
                }


            }
        }, this));
    }

    private void updatePassword(String phone, String password) {
        HttpManager.getInstance().updatePassword(phone, password).compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<CheckBean>() {
            @Override
            public void onNext(CheckBean o) {
                if (pd != null)
                    pd.dismiss();
                if (o.getResult().equals("1")) {

                    MobclickAgent.onEvent(ResetPsActivity.this, "forget_inputPwd_sureBTn_success");
                    AppManager.finishAllActivity();
                    ToastUtils.showToast(ResetPsActivity.this, "密码修改成功");
                    startActivity(new Intent(ResetPsActivity.this, LoginActivity.class));
                    finish();
                }
            }
        }, this));
}

    @Override
    public void newUserRegistration() {

    }

    @Override
    public void registerSuccess(String userId,String flags,String day) {
//        AppManager.finishAllActivity();
        if (pd != null)
            pd.dismiss();
        MobclickAgent.onEvent(ResetPsActivity.this, "reg_inputPwd_suerBtn_success");
        ToastUtils.showToast(this, "注册成功");
        LogUtils.e("注册","注册成功uid:"+userId);
        if(flags.equals("0")){
            TiyanFragment freeFragment = new TiyanFragment();
            Bundle args = new Bundle();
            args.putString("day", day);
            freeFragment.setArguments(args);
            freeFragment.setCancelable(false);
            freeFragment.show(getFragmentManager(),"");
        }else{
            AppManager.finishAllActivity();
            Intent intent = new Intent(ResetPsActivity.this, RegisterLabelActivity.class);
//            intent.putExtra("userId", userId);
            startActivity(intent);
            this.finish();
        }



    }

    @Override
    public void checkCode(SMSbean smSbean) {

    }

    @Override
    public void returnCheck() {
        registerPresenter.newUserRegistration(phoneNum
                , newPassword.getText().toString()
    // 第三个参数 管家编号
                , "");

}

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
